package com.zues.service.impl;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zues.dao.ShopDAO;
import com.zues.dto.ShopExecution;
import com.zues.entity.Shop;
import com.zues.enums.ShopStateEnum;
import com.zues.exception.ShopOperationException;
import com.zues.service.ShopService;
import com.zues.util.ImageUtil;
import com.zues.util.PageCalculator;
import com.zues.util.PathUtil;

@Service
public class ShopServiceImpl implements ShopService {

	@Autowired
	private ShopDAO shopDAO;

	@Override
	@Transactional
	public ShopExecution addShop(Shop shop, InputStream shopImgInputStream, String fileName) {
		// 判断空值
		if (shop == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}
		if (shop.getShopCategory() == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOPCATE);
		}
		if (shop.getArea() == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOPAREA);
		}
		// 给店铺信息赋初始值
		try {
			shop.setEnableStatus(0);
			shop.setCreateTime(new Date());
			shop.setLastEditTime(new Date());
			// 添加店铺信息
			int effectedNum = shopDAO.insertShop(shop);
			if (effectedNum <= 0) {
				throw new ShopOperationException("店铺创建失败");
			} else {
				if (shopImgInputStream != null) {
					// 存储图片
					try {
						addShopImg(shop, shopImgInputStream,fileName);
					} catch (Exception e) {

						throw new ShopOperationException("addShop error :" + e.getMessage());
					}
					// 更新店铺的图片地址
					effectedNum = shopDAO.updateShop(shop);
					if (effectedNum <= 0) {
						throw new ShopOperationException("更新图片地址失败");
					}
				}
			}
		} catch (Exception e) {
			throw new ShopOperationException("addShop error :" + e.getMessage());
		}
		return new ShopExecution(ShopStateEnum.CHECK, shop);
	}

	private void addShopImg(Shop shop, InputStream shopImgInputStream,String fileName) {
		// 获取shop图片目录的相对值路径
		String dest = PathUtil.getShopImagePath(shop.getShopId());
		String shopImgAddr = ImageUtil.generateThumbnail(shopImgInputStream, fileName, dest);
		shop.setShopImg(shopImgAddr);
	}

	@Override
	public Shop getByShopid(long shopId) {
		return shopDAO.queryByShopId(shopId);
	}

	@Override
	public ShopExecution modifyShop(Shop shop, InputStream shopImgInputStream, String fileName) throws ShopOperationException{
		if(shop == null || shop.getShopId()==null ){
			return new ShopExecution(ShopStateEnum.NULL_SHOPID);
		}else{
		//1.判断是否需要处理图片
			try{
			if(shopImgInputStream!=null && fileName!=null){
				Shop tempShop = shopDAO.queryByShopId(shop.getShopId());
				if(tempShop.getShopImg()!=null){
					ImageUtil.deleteFileOrPath(tempShop.getShopImg());
				}
				addShopImg                                                            (shop, shopImgInputStream, fileName);
			}
		//2.更新店铺信息
			shop.setLastEditTime(new Date());
			int effectedNum = shopDAO.updateShop(shop);
			if(effectedNum <= 0){
				return  new ShopExecution(ShopStateEnum.INNER_ERROR);
			}else{
				shop = shopDAO.queryByShopId(shop.getShopId());
				return new ShopExecution(ShopStateEnum.SUCCESS,shop);
			}
			}catch (Exception e) {
				throw new ShopOperationException("modifyShop err:"+e.getMessage());
			}
		}
	}

	@Override
	public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		List<Shop> shopList = shopDAO.queryShopList(shopCondition, rowIndex, pageSize);
		int count = shopDAO.queryShopCount(shopCondition);
		ShopExecution shopExecution = new ShopExecution();
		if(shopList != null){
			shopExecution.setShopList(shopList);
			shopExecution.setCount(count);
		}else{
			shopExecution.setState(ShopStateEnum.INNER_ERROR.getState());
		}
		return shopExecution;
	}

}
